package com.example.ruralatour.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.vo.Result;
import com.example.ruralatour.entity.Orders;
import com.example.ruralatour.service.IOrdersService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangzhen
 * @since 2024-05-18
 */
@RestController
@RequestMapping("/order")
//@RequiredArgsConstructor
public class OrdersController {
    @Autowired
    private IOrdersService ordersService;
    @GetMapping("/list")
    public Result<?> getOrderListPage(@RequestParam(value = "userId", required = false) Integer userId,
                                      @RequestParam(value = "orderType", required = false) String orderType,
                                      @RequestParam("pageNo") Long pageNo,
                                      @RequestParam("pageSize") Long pageSize){

        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        if (userId != null){
            wrapper.eq("userId", userId);
        }

        if (orderType != null){
            wrapper.eq("orderType", orderType);
        }

        //wrapper.orderByDesc("orderId");
        Page<Orders> page = new Page<>(pageNo, pageSize);
        ordersService.page(page, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }
    @PostMapping("/addOrder")
    public Result<?> addOrder(@RequestBody Orders order){
        ordersService.save(order);
       // System.out.println(order);
        return Result.success("添加订单成功");
    }

    //修改订单存疑
    //@PutMapping
    @PostMapping("/updateOrder")
    public Result<?> updateOrder(@RequestBody Orders order){
        ordersService.updateById(order);
        return Result.success("修改订单成功");
    }

    @GetMapping("/getOrderById/{id}")
    public Result<Orders> getOrderById(@PathVariable("id") Integer orderId){
        Orders Orders=ordersService.getById(orderId);
        return Result.success(Orders);
    }

    @DeleteMapping("/deleteOrderById")
    public Result<Orders> deleteOrderById(@RequestParam("deleteOrderId") Integer id){
        ordersService.removeById(id);
        return Result.success("删除订单成功");
    }

}
